﻿using Ledict.ConditionExtract.Base;
using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Ledict.Utility.Msg;
using System.Text.RegularExpressions;
using System.Text;
using Ledict.ConditionExtract.Frm.NodeTree;
using Ledict.ConditionExtract.Frm;
using Ledict.Utility.Helper;

namespace Ledict.ConditionExtract
{
    /// <summary>
    /// 引用法规
    /// </summary>
    public partial class AboutLaw1 : BaseForm
    {
        #region 构造单例
        public AboutLaw1()
        {
            InitializeComponent();
        }
        static AboutLaw1 _default;
        public static AboutLaw1 Default
        {
            get
            {
                if (_default == null || _default.IsDisposed)
                {
                    _default = new AboutLaw1();
                }
                return _default;
            }
        }
        #endregion

        TreeDataBLL trbll = new TreeDataBLL();
        NewIDBLL newid = new NewIDBLL();
        CaseCauseBLL casecausebLL = new CaseCauseBLL();
        QuoteLawRelBLL quoteLawRelBLL = new QuoteLawRelBLL();

        string QLC_ID1 = string.Empty;

        #region 共有变量
        private TreeNode clicknode = null;

        private List<TreeNode> NodeList = new List<TreeNode>();
        //private List<TreeNode> SourceNodeList = new List<TreeNode>();
        public string TypeName = "刑事";
        public string FirId = "1000";
        public string LocateNode = "";
        private CaseCauseBLL ccb = new CaseCauseBLL();
        private string tabPageName = "单一";
        #endregion

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AboutLaw_Load(object sender, EventArgs e)
        {
            CaseCauseTree();
        }


        #region 左侧
        #region 左侧树      

        #region 情形情节节点树
        /// <summary>
        /// 案由树（第一级）
        /// </summary>
        public void CaseCauseTree()
        {
            #region 初始化
            this.tVCaseCause.Nodes.Clear();
            NodeList = new List<TreeNode>();

            StacticQuery.Text = "";
            CurrNodeNum.Text = "";
            SumQuery.Text = "";
            #endregion
            DataTable dt = trbll.NodeTree_Tree("");
            DataRow[] arrayDr = new DataRow[] { };
            if (TypeName == "刑事")
            {
                arrayDr = dt.Select("name='" + TypeName + "罪名" + "' or " + "name='" + TypeName + "程序法" + "'");
            }
            else if (TypeName == "行政执法")
            {
                arrayDr = dt.Select("name='" + TypeName + "' or " + "name='" + TypeName + "程序法" + "'");
            }
            else
            {
                arrayDr = dt.Select("name='" + TypeName + "案由" + "' or " + "name='" + TypeName + "程序法" + "'");
            }
            foreach (DataRow caseTreeNodeRode in arrayDr)
            {
                TreeNode node = new TreeNode();
                node.Text = LdFormat.ObjectToString(caseTreeNodeRode[1]);
                //if (TypeName == "刑事")
                //{
                //    node.Text = LdFormat.ObjectToString(caseTreeNodeRode[1]).Replace("刑事罪名", "实体法");
                //}
                //else if (TypeName == "民事")
                //{
                //    node.Text = LdFormat.ObjectToString(caseTreeNodeRode[1]).Replace("民事案由", "实体法");
                //}
                node.Tag = caseTreeNodeRode[0];
                node.Name = LdFormat.ObjectToString(caseTreeNodeRode[2]);
                FirId = caseTreeNodeRode[0].ToString();
                if (trbll.NodeTree_Tree(node.Tag.ToString()).Rows.Count > 0)
                {
                    node.Nodes.Add(new TreeNode());
                }
                tVCaseCause.Nodes.Add(node);
                NodeList.Add(node);
            }
        }

        /// <summary>
        /// 查询节点的下级树
        /// </summary>
        /// <param name="parentNode">父节点</param>
        private void CaseCauseTreeChild(TreeNode parentNode)
        {
            string pid = parentNode.Tag.ToString();
            DataTable caseTreeNodeRode = trbll.NodeTree_Tree(pid);
            if (caseTreeNodeRode.Rows.Count == 0)
                return;
            for (int i = 0; i < caseTreeNodeRode.Rows.Count; i++)
            {
                TreeNode node = new TreeNode();
                node.Tag = caseTreeNodeRode.Rows[i]["id"].ToString();
                node.Text = LdFormat.ObjectToString(caseTreeNodeRode.Rows[i]["name"]);

                node.Name = LdFormat.ObjectToString(caseTreeNodeRode.Rows[i]["CC_NodeType"]);
                if (trbll.NodeTree_Tree(node.Tag.ToString()).Rows.Count > 0)
                {
                    node.Nodes.Add(new TreeNode());
                }
                parentNode.Nodes.Add(node);
                TreeNode node1 = null;
                foreach (var node2 in NodeList)//遍历现在界面上的所有节点
                {
                    if (node2.Tag.ToString() == node.Tag.ToString())
                    {
                        node1 = node2;
                        break;
                    }
                }
                if (node1 != null)
                {
                    NodeList.Remove(node1);
                    NodeList.Add(node);//list用来查询
                }
                else
                {
                    NodeList.Add(node);
                }
            }
        }

        /// <summary>
        /// 展开节点
        /// </summary>
        /// <param name="sender">触发对象</param>
        /// <param name="e">节点</param>
        private void tVCaseCause_AfterExpand(object sender, TreeViewEventArgs e)
        {
            e.Node.Nodes.Clear();
            CaseCauseTreeChild(e.Node);
        }

        /// <summary>
        /// 点击节点
        /// </summary>
        /// <param name="sender">触发对象</param>
        /// <param name="e">节点</param>
        private void tVCaseCause_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (clicknode != null)
            {
                clicknode.ForeColor = Color.Black;
            }
            if (e.Node != null) //是否有效节点
            {
                clicknode = e.Node;
                clicknode.ForeColor = Color.Red;
                GetRightValue(e);
                txbConditionName.Text = txbConditionExplain.Text = "";
                colFileter1.SetFileter("");
            }
        }

        #endregion

        #endregion

        #region 民事刑事按钮（显示不同树）
        /// <summary>
        /// 按钮默认颜色全部为白色
        /// </summary>
        public void GetColor()
        {
            button8.BackColor = Color.White;
            button6.BackColor = Color.White;
            button5.BackColor = Color.White;
            button4.BackColor = Color.White;
            button7.BackColor = Color.White;
            btnXZZF.BackColor = Color.White;
        }
        /// <summary>
        /// 点击“刑事”，显示刑事树
        /// </summary>
        /// <param name="sender">触发对象</param>
        /// <param name="e">节点</param>
        private void button8_Click(object sender, EventArgs e)
        {
            TypeName = "刑事";
            GetColor();
            Button sendButton = (Button)sender;
            sendButton.BackColor = Color.LightSkyBlue;
            CaseCauseTree();


        }

        /// <summary>
        /// 点击“民事”，显示民事树
        /// </summary>
        /// <param name="sender">触发对象</param>
        /// <param name="e">节点</param>
        private void button6_Click(object sender, EventArgs e)
        {
            TypeName = "民事";
            GetColor();
            Button sendButton = (Button)sender;
            sendButton.BackColor = Color.LightSkyBlue;
            CaseCauseTree();

        }

        /// <summary>
        /// 点击“行政"，显示行政树
        /// </summary>
        /// <param name="sender">触发对象</param>
        /// <param name="e">节点</param>
        private void button5_Click(object sender, EventArgs e)
        {
            TypeName = "行政";
            GetColor();
            Button sendButton = (Button)sender;
            sendButton.BackColor = Color.LightSkyBlue;
            CaseCauseTree();


        }

        /// <summary>
        /// 点击“赔偿"，显示赔偿树
        /// </summary>
        /// <param name="sender">触发对象</param>
        /// <param name="e">节点</param>
        private void button4_Click(object sender, EventArgs e)
        {
            TypeName = "赔偿";
            GetColor();
            Button sendButton = (Button)sender;
            sendButton.BackColor = Color.LightSkyBlue;
            CaseCauseTree();

        }

        /// <summary>
        /// 点击“执行"，显示执行树
        /// </summary>
        /// <param name="sender">触发对象</param>
        /// <param name="e">节点</param>
        private void button7_Click(object sender, EventArgs e)
        {
            TypeName = "执行";
            GetColor();
            Button sendButton = (Button)sender;
            sendButton.BackColor = Color.LightSkyBlue;
            CaseCauseTree();

        }
        #endregion

        #region 编辑案由树节点

        private TreeNode editnode = null; //点击获得当前节点
        /// <summary>
        /// 显示编辑菜单
        /// </summary>
        /// <param name="sender">触发对象</param>
        /// <param name="e">节点</param>
        private void tVCaseCause_MouseDown(object sender, MouseEventArgs e)
        {

            Point ClickPoint = new Point(e.X, e.Y);
            editnode = tVCaseCause.GetNodeAt(ClickPoint);
            if (editnode != null)//判断你点的是不是一个节点
            {
                editnode.ContextMenuStrip = contextMenuStrip1;
                tVCaseCause.SelectedNode = editnode;  //选中这个节点

            }

        }


        DataTable Typedt = new DataTable();

        #region 查询节点
        DataTable Querydt;
        private void QueryNodeTxt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                QueryNode();
            }
        }

        private void QueryNode()
        {
            string QueryNodeName = QueryNodeTxt.Text.Trim();

            if (QueryNodeName != "")
            {
                Querydt = trbll.NodeTree_Query(FirId, QueryNodeName, 1);
                StacticQuery.Text = Querydt.Rows.Count.ToString();
                if (Querydt.Rows.Count > 0)
                {
                    CurrNodeNum.Text = "1";
                    SumQuery.Text = CurrNodeNum.Text;
                    int CurrNode = Convert.ToInt32(CurrNodeNum.Text);
                    LinkIsVisible(CurrNode);
                    QueryNode_Page(Querydt, CurrNode);
                }
                else
                {
                    CurrNodeNum.Text = "0";
                    SumQuery.Text = CurrNodeNum.Text;
                    MessageBox.Show("没有查询到结果！");
                }
            }
            else
            {
                MessageBox.Show("请输入关键字！");
            }
        }

        /// <summary>
        /// 查询按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryNodeBtn_Click(object sender, EventArgs e)
        {
            QueryNode();
        }
        ///// <summary>
        ///// 下一条
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void Nextlinkbtn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        //{
        //    CurrNodeNum.Text = (Convert.ToInt32(CurrNodeNum.Text) + 1).ToString();
        //    SumQuery.Text = CurrNodeNum.Text;
        //    int CurrNode = Convert.ToInt32(CurrNodeNum.Text);
        //    QueryNode_Page(Querydt, CurrNode);
        //    LinkIsVisible(CurrNode);
        //}
        ///// <summary>
        ///// 上一条
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void Prelinkbtn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        //{
        //    CurrNodeNum.Text = (Convert.ToInt32(CurrNodeNum.Text) - 1).ToString();
        //    SumQuery.Text = CurrNodeNum.Text;
        //    int CurrNode = Convert.ToInt32(CurrNodeNum.Text);
        //    QueryNode_Page(Querydt, CurrNode);
        //    LinkIsVisible(CurrNode);
        //}
        /// <summary>
        /// 查询分页
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="showindex"></param>
        private void QueryNode_Page(DataTable dt, int showindex)
        {
            if (dt.Rows.Count >= showindex)
            {
                string[] NotePidArr = trbll.GetAllParentNode(dt.Rows[showindex - 1]["id"].ToString()).Split(',');
                for (int i = 0; i < NotePidArr.Length; i++)
                {
                    if (i != NotePidArr.Length - 1)
                    {
                        //FindNode(NotePidArr[i]).Expand();

                        var Node = FindNode(NotePidArr[i]);
                        if (Node != null)
                        {
                            Node.Expand();//拖出所有父级节点
                        }
                    }
                    else
                    {
                        TreeNode node = FindNode(NotePidArr[i]);
                        this.tVCaseCause.SelectedNode = node;
                    }
                }
            }
            else
            {
                MessageBox.Show("跳转页码有误，重新输入！");
            }
        }
        /// <summary>
        /// 找到节点
        /// </summary>
        /// <param name="Nodeid"></param>
        /// <returns></returns>
        private TreeNode FindNode(string Nodeid)
        {
            foreach (TreeNode node in NodeList)
            {
                if (node.Tag.ToString() == Nodeid)
                {
                    return node;
                }
            }
            return null;

        }
        /// <summary>
        /// 上一条下一条是否可用
        /// </summary>
        /// <param name="CurrNode"></param>
        private void LinkIsVisible(int CurrNode)
        {
            Prelinkbtn.Enabled = true;
            Nextlinkbtn.Enabled = true;
            if (CurrNode <= 1)
            {
                Prelinkbtn.Enabled = false;
            }
            if (CurrNode >= Querydt.Rows.Count)
            {
                Nextlinkbtn.Enabled = false;
            }

        }
        ///// <summary>
        ///// 跳页查询
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void GoQuery_Click(object sender, EventArgs e)
        //{
        //    int CurrNode = Convert.ToInt32(SumQuery.Text);

        //    if (Querydt.Rows.Count >= CurrNode)
        //    {
        //        CurrNodeNum.Text = SumQuery.Text;
        //        QueryNode_Page(Querydt, CurrNode);
        //        LinkIsVisible(CurrNode);
        //    }
        //    else
        //    {
        //        MessageBox.Show("跳转页码有误，重新输入！");
        //    }

        //}

        #endregion

        #endregion

        #endregion

        #region 右侧
        private void ShowTab_RelLaw()
        {
            if (clicknode != null)
            {
                ShowRelationLaw(clicknode.Tag.ToString());  //显示相关法规
            }
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="e"></param>
        public void GetRightValue(TreeViewEventArgs e)
        {
            ShowTab_RelLaw();
        }

        #region 相关法条（第三个插页）
        /// <summary>
        /// 显示来源列表
        /// </summary>
        /// <param name="CC_ID"></param>
        private void ShowRelationLaw(string CC_ID)
        {
            if (tabPageName == "单一")
            {
                DV_QuoteLawRel.AutoGenerateColumns = false;
                DV_QuoteLawRel.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                ProvText.Text = "";
                DV_QuoteLawReldataPager1.ChangePageNumber(1);
            }
            else if (tabPageName == "组合")
            {
                DV_QuoteLawRelZH.AutoGenerateColumns = false;
                DV_QuoteLawRelZH.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                ProvText1.Text = "";
                DV_QuoteLawReldataPager1.ChangePageNumber(1);
                //dataPager1.ChangePageNumber(1);
            }
            else if (tabPageName == "主题词关联法条")
            {
                tvZTC.Nodes.Clear();
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                richTextBoxFTContent.Text = "";
                string headwordID = quoteLawRelBLL.GetHeadwordInID(CC_ID);
                if (!string.IsNullOrEmpty(headwordID))
                {
                    DataTable dt = quoteLawRelBLL.GetData(headwordID);
                    DataTable dtquote = quoteLawRelBLL.GetQuoteLawSet();
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        dt.Columns.Add("right");
                        dt.Columns.Add("wrong");
                        dt.Columns.Add("Num");
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            dt.Rows[i]["Num"] = i + 1;
                            string ProvHwordInID = dt.Rows[i]["ProvHwordInID"].ToString();
                            if (dtquote != null && dtquote.Rows.Count > 0)
                            {
                                if (dtquote.Select(" ProvHwordInID='" + ProvHwordInID + "' ").Length > 0)
                                {
                                    dt.Rows[i]["right"] = Convert.ToInt32(dtquote.Select(" ProvHwordInID='" + ProvHwordInID + "' ")[0]["checkright"]) == 1 ? true : false;
                                    dt.Rows[i]["wrong"] = Convert.ToInt32(dtquote.Select(" ProvHwordInID='" + ProvHwordInID + "' ")[0]["checkwrong"]) == 1 ? true : false;
                                }
                            }
                        }
                        dt.DefaultView.Sort = " right asc,wrong asc ";
                        dt = dt.DefaultView.ToTable();
                        dataGridView1.DataSource = dt;
                    }
                }
                else
                {
                    if (dataGridView1.DataSource != null)
                    {
                        DataTable dt1 = dataGridView1.DataSource as DataTable;
                        dt1.Rows.Clear();
                        dataGridView1.DataSource = dt1;
                    }
                }
                //dataPager1.ChangePageNumber(1);
            }
        }

        DataTable dt = new DataTable();
        private void DV_QuoteLawReldataPager1_PageNumberChanging(object sender, Controls.DataPagerPageIndexChangingEventArgs e)
        {
            if (clicknode != null)
            {

                var startRowIndex = e.StartRowIndex;
                //var maximumRows = e.MaximumRows;
                var endRowIndex = e.StartRowIndex + e.MaximumRows;
                if (tabPageName == "单一")
                {
                    if (TypeName == "刑事" || TypeName == "行政执法")
                    {
                        dt = quoteLawRelBLL.QuoteLawRelGetList(clicknode.Tag.ToString());
                        lujing.Visible = Describe.Visible = false;
                    }
                    else if (TypeName == "民事")
                    {
                        lujing.Visible = Describe.Visible = true;
                        dt = quoteLawRelBLL.QuoteLawRelGetListByMinShi(clicknode.Tag.ToString());
                    }
                }
                else if (tabPageName == "组合")
                {
                    dt = quoteLawRelBLL.QuoteLawRelByCombiationLaw(clicknode.Name == "2" ? clicknode.Tag.ToString() : ccb.IsParentRealation1(clicknode.Tag.ToString()).CC_ID);
                    lujing.Visible = Describe.Visible = false;
                }
                if (dt.Rows.Count > 0)
                {
                    if (tabPageName == "单一")
                    {
                        DV_QuoteLawRel.DataSource = dt;
                    }
                    else if (tabPageName == "组合")
                    {
                        DV_QuoteLawRelZH.DataSource = dt;
                    }
                }
                else
                {
                    if (tabPageName == "单一")
                    {
                        DV_QuoteLawRel.DataSource = new DataTable();
                    }
                    else if (tabPageName == "组合")
                    {
                        DV_QuoteLawRelZH.DataSource = new DataTable();
                    }
                }
            }

        }

        /// <summary>
        /// 弹出页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RelLaw_Case_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (DataGridViewColumn item in DV_QuoteLawRel.Columns)
            {
                sb.Append(item.HeaderText + " ");
            }
            string str = sb.ToString();

            NodeTree_Case1 quoteLawRelAboutCase = new NodeTree_Case1();
            quoteLawRelAboutCase.stiuationId = clicknode.Tag.ToString();  // clicknode.Tag.ToString()
            quoteLawRelAboutCase.stiuationName = clicknode.Text.ToString();
            quoteLawRelAboutCase.stiuationType = clicknode.Name.ToString();
            SYS_CaseCause obj = ccb.GetCaseCauseByCCID(quoteLawRelAboutCase.stiuationId);
            string fullpath = obj.XmlPath;
            quoteLawRelAboutCase.xmlPath = fullpath;
            if (TypeName == "刑事")
            {
                quoteLawRelAboutCase.ccName = "刑事";
            }
            else if (TypeName == "民事")
            {
                quoteLawRelAboutCase.ccName = "民事";
                if (string.IsNullOrEmpty(quoteLawRelAboutCase.xmlPath))
                {
                    MsgBox.Warn("该节点没有xpath不能添加引用法规");
                    return;
                }
            }
            if (clicknode.Parent != null)
                quoteLawRelAboutCase.pid = clicknode.Parent.Tag.ToString();
            else quoteLawRelAboutCase.pid = "";
            quoteLawRelAboutCase.ShowDialog();
            ShowTab_RelLaw();


        }


        /// <summary>
        /// 删除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteProv_Click(object sender, EventArgs e)
        {
            var DeleteES_ID = "";
            int num = 0;
            for (var i = 0; i <= (tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows.Count - 1; i++)
            {
                var check = (DataGridViewCheckBoxCell)(tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[i].Cells[0];
                if (Convert.ToBoolean(check.Value))
                {
                    num++;
                }
            }
            if (num == 0)
            {
                MsgBox.Warn("请勾选需要删除的数据后再试");
                return;
            }
            if (MessageBox.Show("确认要删除数据吗？", "此删除不可恢复", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    bool res = false;
                    for (var i = 0; i <= (tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows.Count - 1; i++)
                    {
                        var check = (DataGridViewCheckBoxCell)(tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[i].Cells[0];
                        if (Convert.ToBoolean(check.Value))
                        {
                            DeleteES_ID = tabPageName == "单一" ? this.DV_QuoteLawRel.Rows[i].Cells["QLR_ID"].Value.ToString() : this.DV_QuoteLawRelZH.Rows[i].Cells["QLR_IDZH"].Value.ToString();
                            if (DeleteES_ID != "")
                            {
                                res = quoteLawRelBLL.Delete(DeleteES_ID);
                            }
                            if (TypeName == "民事")
                            {
                                SYS_Label_LabelRelBLL bll = new SYS_Label_LabelRelBLL();
                                string OLR_SituationID = DV_QuoteLawRel.Rows[i].Cells["idd"].Value.ToString();
                                res = bll.Delete(OLR_SituationID);
                            }
                        }
                    }
                    if (res)
                    {
                        MsgBox.OK("删除成功!");
                    }
                    else
                    {
                        MsgBox.Warn("删除失败!");
                    }
                }
                catch (Exception error)
                {
                    MsgBox.Warn("删除失败!");
                }
                ShowRelationLaw(clicknode.Tag.ToString());
            }
        }




        #endregion

        #endregion

        /// <summary>
        /// 上一条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Prelinkbtn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (!String.IsNullOrEmpty(CurrNodeNum.Text))
            {
                CurrNodeNum.Text = (Convert.ToInt32(CurrNodeNum.Text) - 1).ToString();//上一条
                SumQuery.Text = CurrNodeNum.Text;
                var CurrNode = Convert.ToInt32(CurrNodeNum.Text);
                QueryNode_Page(Querydt, CurrNode);//定位查询
                LinkIsVisible(CurrNode);
            }
        }

        /// <summary>
        /// 全部展开按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            var findNode = FindNode(editnode.Tag.ToString());
            if (findNode != null) findNode.ExpandAll();
        }

        /// <summary>
        /// 下一条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Nextlinkbtn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (!string.IsNullOrEmpty(CurrNodeNum.Text))
            {
                CurrNodeNum.Text = (Convert.ToInt32(CurrNodeNum.Text) + 1).ToString();//下一条
                SumQuery.Text = CurrNodeNum.Text;
                var CurrNode = Convert.ToInt32(CurrNodeNum.Text);
                QueryNode_Page(Querydt, CurrNode);//定位查询
                LinkIsVisible(CurrNode);
            }
        }

        /// <summary>
        /// 跳转输入框只能输入数字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SumQuery_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
        }

        /// <summary>
        /// 跳页查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GoQuery_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(SumQuery.Text))
            {
                var CurrNode = Convert.ToInt32(SumQuery.Text);//当前节点条数
                if (Querydt != null && !string.IsNullOrEmpty(QueryNodeTxt.Text.Trim()))
                {
                    if (Querydt.Rows.Count >= CurrNode)
                    {
                        CurrNodeNum.Text = SumQuery.Text;//跳转
                        QueryNode_Page(Querydt, CurrNode);//定位到目标节点
                        LinkIsVisible(CurrNode);// 上一条下一条是否可用
                    }
                    else
                    {
                        MessageBox.Show("跳转页码有误，重新输入！");
                    }
                }
            }
            else
            {
                MsgBox.Warn("跳转页码为空，请重新输入！");
            }
        }

        /// <summary>
        /// 定位按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LocateWord_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (tabPageName == "单一")
            {
                ProvText.SelectionStart = 0;
                ProvText.SelectionLength = ProvText.Text.Length;
                ProvText.SelectionBackColor = Color.White;
                MatchCollection mc;
                var r = new System.Text.RegularExpressions.Regex(SelectWord.Text.Trim()); //定义一个Regex对象实例
                mc = r.Matches(ProvText.Text);
                for (var i = 0; i < mc.Count; i++) //在输入字符串中找到所有匹配
                {
                    var start = mc[i].Index; //记录匹配字符的位置
                    var length = (SelectWord.Text.Trim()).Length;
                    ProvText.SelectionStart = start;
                    ProvText.SelectionLength = length;
                    ProvText.SelectionBackColor = Color.LightSkyBlue;
                }
            }
            else if (tabPageName == "组合")
            {
                ProvText1.SelectionStart = 0;
                ProvText1.SelectionLength = ProvText1.Text.Length;
                ProvText1.SelectionBackColor = Color.White;
                MatchCollection mc;
                var r = new System.Text.RegularExpressions.Regex(SelectWord1.Text.Trim()); //定义一个Regex对象实例
                mc = r.Matches(ProvText1.Text);
                for (var i = 0; i < mc.Count; i++) //在输入字符串中找到所有匹配
                {
                    var start = mc[i].Index; //记录匹配字符的位置
                    var length = (SelectWord1.Text.Trim()).Length;
                    ProvText1.SelectionStart = start;
                    ProvText1.SelectionLength = length;
                    ProvText1.SelectionBackColor = Color.LightSkyBlue;
                }
            }
        }

        /// <summary>
        /// 点击数据显示该数据关联法条信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DV_QuoteLawRel_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (tabPageName == "组合")
                {
                    txbConditionName.Text = DV_QuoteLawRelZH.Rows[e.RowIndex].Cells["QLC_Name"].Value.ToString();
                    colFileter1.SetFileter(DV_QuoteLawRelZH.Rows[e.RowIndex].Cells["QLC_Content"].Value.ToString());
                    txbConditionExplain.Text = DV_QuoteLawRelZH.Rows[e.RowIndex].Cells["QLC_Explain"].Value.ToString();
                    QLC_ID1 = DV_QuoteLawRelZH.Rows[e.RowIndex].Cells["dataGridViewTextBoxColumn18"].Value.ToString();

                    string OLR_Law_ProvID = DV_QuoteLawRelZH.Rows[e.RowIndex].Cells["dataGridViewTextBoxColumn21"].Value.ToString();
                    ProvText1.Text = quoteLawRelBLL.GetProvText(OLR_Law_ProvID);
                    SelectWord1.Text = SelectWord.Text = "";
                }
                else if (tabPageName == "单一")
                {
                    ProvText.Text = "";
                    string ProvID = DV_QuoteLawRel.Rows[e.RowIndex].Cells["OLR_Law_ProvID"].Value.ToString();
                    string Clause = DV_QuoteLawRel.Rows[e.RowIndex].Cells["OLR_Law_ClauseNumber"].Value.ToString();
                    string Item = DV_QuoteLawRel.Rows[e.RowIndex].Cells["OLR_Law_ItemNumber"].Value.ToString();
                    if (!string.IsNullOrEmpty(Item))//获取项信息
                    {
                        ProvText.Text = quoteLawRelBLL.GetItemText(ProvID, Clause, Item);
                    }
                    else if (!string.IsNullOrEmpty(Clause))//获取款信息
                    {
                        ProvText.Text = quoteLawRelBLL.GetClauseText(ProvID, Clause);
                    }
                    else if (!string.IsNullOrEmpty(ProvID))//获取条信息
                    {
                        ProvText.Text = quoteLawRelBLL.GetProvText(ProvID);
                    }
                    SelectWord1.Text = SelectWord.Text = "";
                }
            }
        }

        private void DV_QuoteLawRel_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //foreach (DataGridViewRow row in DV_QuoteLawRel.Rows)
            //{
            //    row.Cells[0].Value = row.Index + 1;
            //}
        }

        private void DV_QuoteLawRel_KeyDown(object sender, KeyEventArgs e)
        {

            int row = (tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).CurrentRow.Index;//获取当前选中的行数
            int roww = (tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows.Count - 1;//获取最后一行
            if (e.KeyCode == Keys.Down)//键盘按方向下
            {
                if (row == roww)
                {
                    string OLR_Law_ProvID = (tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[row].Cells["OLR_Law_ProvID"].Value.ToString();
                    ProvText.Text = quoteLawRelBLL.GetProvText(OLR_Law_ProvID);
                    SelectWord1.Text = SelectWord.Text = "";
                }
                else
                {
                    string OLR_Law_ProvID = (tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[row + 1].Cells["OLR_Law_ProvID"].Value.ToString();
                    ProvText.Text = quoteLawRelBLL.GetProvText(OLR_Law_ProvID);
                    SelectWord1.Text = SelectWord.Text = "";
                }
            }
            else if (e.KeyCode == Keys.Up)//键盘按方向上
            {
                if (row - 1 < 0)
                {
                    string OLR_Law_ProvID = (tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[0].Cells["OLR_Law_ProvID"].Value.ToString();
                    ProvText.Text = quoteLawRelBLL.GetProvText(OLR_Law_ProvID);
                    SelectWord1.Text = SelectWord.Text = "";
                }
                else
                {
                    string OLR_Law_ProvID = (tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[row - 1].Cells["OLR_Law_ProvID"].Value.ToString();
                    ProvText.Text = quoteLawRelBLL.GetProvText(OLR_Law_ProvID);
                    SelectWord1.Text = SelectWord.Text = "";
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (DataGridViewColumn item in DV_QuoteLawRel.Columns)
            {
                sb.Append(item.HeaderText + " ");
            }
            string str = sb.ToString();

            NodeTree_Case1 quoteLawRelAboutCase = new NodeTree_Case1();
            quoteLawRelAboutCase.stiuationId = clicknode.Tag.ToString();  // clicknode.Tag.ToString()
            quoteLawRelAboutCase.stiuationName = clicknode.Text.ToString();
            quoteLawRelAboutCase.stiuationType = clicknode.Name.ToString();
            SYS_CaseCause obj = ccb.GetCaseCauseByCCID(quoteLawRelAboutCase.stiuationId);
            string fullpath = obj.XmlPath;
            quoteLawRelAboutCase.xmlPath = fullpath;
            quoteLawRelAboutCase.ccName = "组合";
            if (clicknode.Parent != null)
                quoteLawRelAboutCase.pid = clicknode.Parent.Tag.ToString();
            else quoteLawRelAboutCase.pid = "";
            quoteLawRelAboutCase.ShowDialog();
            ShowTab_RelLaw();

        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //StringBuilder sb = new StringBuilder();
            //foreach (DataGridViewColumn item in DV_QuoteLawRel.Columns)
            //{
            //    sb.Append(item.HeaderText + " ");
            //}
            //string str = sb.ToString();

            NodeTree_Case1 quoteLawRelAboutCase = new NodeTree_Case1();
            quoteLawRelAboutCase.stiuationId = clicknode.Tag.ToString();  // clicknode.Tag.ToString()
            quoteLawRelAboutCase.stiuationName = clicknode.Text.ToString();
            quoteLawRelAboutCase.stiuationType = clicknode.Name.ToString();
            //quoteLawRelAboutCase.tabPageName = tabPageName;
            SYS_CaseCause obj = ccb.GetCaseCauseByCCID(quoteLawRelAboutCase.stiuationId);
            string fullpath = obj.XmlPath;
            quoteLawRelAboutCase.xmlPath = fullpath;
            //if (tabPageName == "单一")
            //{
            if (TypeName == "刑事")
            {
                quoteLawRelAboutCase.ccName = "刑事";
            }
            else if (TypeName == "民事")
            {
                quoteLawRelAboutCase.ccName = "民事";
                //if (string.IsNullOrEmpty(quoteLawRelAboutCase.xmlPath))
                //{
                //    MsgBox.Warn("该节点没有xpath不能添加引用法规");
                //    return;
                //}
            }
            if (TypeName == "行政执法")
            {
                quoteLawRelAboutCase.ccName = "行政执法";
            }
            //}
            //else if (tabPageName == "组合")
            //{
            //    if (TypeName == "刑事")
            //    {
            //        quoteLawRelAboutCase.ccName = "刑事";
            //    }
            //    else if (TypeName == "民事")
            //    {
            //        quoteLawRelAboutCase.ccName = "民事";
            //    }
            //}
            if (clicknode.Parent != null)
                quoteLawRelAboutCase.pid = clicknode.Parent.Tag.ToString();
            else quoteLawRelAboutCase.pid = "";
            quoteLawRelAboutCase.ShowDialog();
            ShowTab_RelLaw();
        }

        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            int num = 0;
            for (var i = 0; i <= (tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows.Count - 1; i++)
            {
                var check = tabPageName == "单一" ? (DataGridViewCheckBoxCell)this.DV_QuoteLawRel.Rows[i].Cells["CheckRow"] : (DataGridViewCheckBoxCell)this.DV_QuoteLawRelZH.Rows[i].Cells["dataGridViewCheckBoxColumn2"];
                if (Convert.ToBoolean(check.Value))
                {
                    num++;
                }
            }
            if (num == 0)
            {
                MsgBox.Warn("请勾选需要修改的数据后再试");
                return;
            }
            try
            {
                SYS_QuoteLawRel sqlModel = new SYS_QuoteLawRel();
                bool res = false;
                for (var i = 0; i <= (tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows.Count - 1; i++)
                {
                    // var check = (DataGridViewCheckBoxCell)(tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRel1).Rows[i].Cells["CheckRow"];

                    var check = tabPageName == "单一" ? (DataGridViewCheckBoxCell)this.DV_QuoteLawRel.Rows[i].Cells["CheckRow"] : (DataGridViewCheckBoxCell)this.DV_QuoteLawRelZH.Rows[i].Cells["dataGridViewCheckBoxColumn2"];
                    if (Convert.ToBoolean(check.Value))
                    {
                        sqlModel.QLR_ID = tabPageName == "单一" ? this.DV_QuoteLawRel.Rows[i].Cells["QLR_ID"].Value.ToString() : this.DV_QuoteLawRelZH.Rows[i].Cells["QLR_ID1"].Value.ToString();
                        sqlModel.OLR_Law_ClauseNumber = string.IsNullOrEmpty(Convert.ToString((tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[i].Cells[11].Value)) ? "" : Convert.ToString((tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[i].Cells[11].Value) == "0" ? "" : Convert.ToString((tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[i].Cells[11].Value);
                        sqlModel.OLR_Law_ClauseName = (tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[i].Cells[11].FormattedValue == null ? "" : Convert.ToString((tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[i].Cells[11].FormattedValue);
                        sqlModel.OLR_Law_ItemNumber = string.IsNullOrEmpty(Convert.ToString((tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[i].Cells[12].Value)) ? "" : Convert.ToString((tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[i].Cells[12].Value) == "0" ? "" : Convert.ToString((tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[i].Cells[12].Value);
                        sqlModel.OLR_Law_ItemName = (tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[i].Cells[12].FormattedValue == null ? "" : Convert.ToString((tabPageName == "单一" ? this.DV_QuoteLawRel : this.DV_QuoteLawRelZH).Rows[i].Cells[12].FormattedValue);
                        if (sqlModel.QLR_ID != "")
                        {
                            res = quoteLawRelBLL.Update(sqlModel);
                        }
                    }
                }
                if (res)
                {
                    MsgBox.OK("修改成功!");
                }
                else
                {
                    MsgBox.Warn("修改失败!");
                }
            }
            catch (Exception error)
            {
                MsgBox.Warn("修改失败!");
            }
            ShowRelationLaw(clicknode.Tag.ToString());
        }

        private void tabControl1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            switch (TabControlShow.SelectedTab.Name)
            {
                case "tabPageSingle":
                    tabPageName = "单一";
                    ShowTab_RelLaw();
                    // DV_QuoteLawRel.DataSource=
                    break;
                case "tabPageGroup":
                    tabPageName = "组合";
                    ShowTab_RelLaw();
                    break;
                case "tabPageZTC":
                    tabPageName = "主题词关联法条";
                    ShowTab_RelLaw();

                    break;
            }
        }

        /// <summary>
        /// 组合界面keyDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DV_QuoteLawRel_KeyDown_1(object sender, KeyEventArgs e)
        {
            int row = this.DV_QuoteLawRelZH.CurrentRow.Index;//获取当前选中的行数
            int roww = DV_QuoteLawRelZH.Rows.Count - 1;//获取最后一行
            if (e.KeyCode == Keys.Down)//键盘按方向下
            {
                if (row == roww)
                {
                    string OLR_Law_ProvID = DV_QuoteLawRelZH.Rows[row].Cells["OLR_Law_ProvID"].Value.ToString();
                    ProvText.Text = quoteLawRelBLL.GetProvText(OLR_Law_ProvID);
                }
                else
                {
                    string OLR_Law_ProvID = DV_QuoteLawRelZH.Rows[row + 1].Cells["OLR_Law_ProvID"].Value.ToString();
                    ProvText.Text = quoteLawRelBLL.GetProvText(OLR_Law_ProvID);
                }
            }
            else if (e.KeyCode == Keys.Up)//键盘按方向上
            {
                if (row - 1 < 0)
                {
                    string OLR_Law_ProvID = DV_QuoteLawRelZH.Rows[0].Cells["OLR_Law_ProvID"].Value.ToString();
                    ProvText.Text = quoteLawRelBLL.GetProvText(OLR_Law_ProvID);
                }
                else
                {
                    string OLR_Law_ProvID = DV_QuoteLawRelZH.Rows[row - 1].Cells["OLR_Law_ProvID"].Value.ToString();
                    ProvText.Text = quoteLawRelBLL.GetProvText(OLR_Law_ProvID);

                }
            }
        }

        /// <summary>
        /// 搜索功能事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            StringBuilder sb = new StringBuilder();
            if (TypeName == "刑事")
            {
                foreach (DataGridViewColumn item in DV_QuoteLawRel.Columns)
                {
                    if (item.HeaderText == "法规名称")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("法规名称", item.Name);
                    }
                    else if (item.HeaderText == "条名称")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("条名称", item.Name);
                    }
                    else if (item.HeaderText == "款名称")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("款名称", item.Name);
                    }
                    else if (item.HeaderText == "项名称")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("项名称", item.Name);
                    }
                    else if (item.HeaderText == "法规类别")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("法规类别", item.Name);
                    }
                    else if (item.HeaderText == "标记")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("标记", item.Name);
                    }
                }

            }
            else if (TypeName == "民事")
            {
                foreach (DataGridViewColumn item in DV_QuoteLawRel.Columns)
                {
                    if (item.HeaderText == "法规名称")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("法规名称", item.Name);
                    }
                    else if (item.HeaderText == "条名称")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("条名称", item.Name);
                    }
                    else if (item.HeaderText == "款名称")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("款名称", item.Name);
                    }
                    else if (item.HeaderText == "项名称")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("项名称", item.Name);
                    }
                    else if (item.HeaderText == "法规类别")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("法规类别", item.Name);
                    }
                    else if (item.HeaderText == "标记")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("标记", item.Name);
                    }
                    else if (item.HeaderText == "说明")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("说明", "LLR_LawLabelName");
                    }
                    else if (item.HeaderText == "xpath")
                    {
                        sb.Append(item.HeaderText + ",");
                        dic.Add("xpath", "LLR_FactLabelXpath");
                    }
                }
            }

            string str = sb.ToString();
            Form3 f = new Form3(str);
            if (f.ShowDialog() == DialogResult.Yes)
            {
                string stt = f.Condition;
                if (tabPageName == "单一")
                {
                    if (TypeName == "刑事")
                    {
                        string stt1 = stt.Replace("法规名称", dic["法规名称"]).Replace("条名称", dic["条名称"]).Replace("款名称", dic["款名称"]).Replace("项名称", dic["项名称"]).Replace("法规类别", dic["法规类别"]).Replace("标记", dic["标记"]).ToString();
                        DataRow[] dr = dt.Select(stt1);
                        if (dr.Length > 0)
                        {
                            dt = dt.Select(stt1).CopyToDataTable<DataRow>();
                            DV_QuoteLawRel.DataSource = dt.Select(stt1).CopyToDataTable<DataRow>();
                        }
                        else
                        {
                            DV_QuoteLawRel.DataSource = new DataTable();
                        }
                    }
                    else if (TypeName == "民事")
                    {
                        string stt1 = stt.Replace("法规名称", dic["法规名称"]).Replace("条名称", dic["条名称"]).Replace("款名称", dic["款名称"]).Replace("项名称", dic["项名称"]).Replace("法规类别", dic["法规类别"]).Replace("标记", dic["标记"]).Replace("说明", dic["说明"]).Replace("xpath", dic["xpath"]).ToString();
                        DataRow[] dr = dt.Select(stt1);
                        if (dr.Length > 0)
                        {
                            dt = dt.Select(stt1).CopyToDataTable<DataRow>();
                            DV_QuoteLawRel.DataSource = dt.Select(stt1).CopyToDataTable<DataRow>();
                        }
                        else
                        {
                            DV_QuoteLawRel.DataSource = new DataTable();
                        }
                    }
                    for (int i = 0; i < DV_QuoteLawRel.Rows.Count; i++)
                    {
                        DV_QuoteLawRel.Rows[i].Cells[11].Value = Convert.ToInt32(string.IsNullOrEmpty(dt.Rows[i]["OLR_Law_ClauseNumber"].ToString()) ? 0 : dt.Rows[i]["OLR_Law_ClauseNumber"]);
                        DV_QuoteLawRel.Rows[i].Cells[12].Value = Convert.ToInt32(string.IsNullOrEmpty(dt.Rows[i]["OLR_Law_ItemNumber"].ToString()) ? 0 : dt.Rows[i]["OLR_Law_ItemNumber"]);
                    }
                }
            }
        }


        /// <summary>
        /// 配置条件修改按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfig_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txbConditionName.Text))
            {
                MessageBox.Show("名称不能为空！");
                txbConditionName.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(colFileter1.GetFileter()))
            {
                MessageBox.Show("条件不能为空！");
                //txbConditionContent.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(txbConditionExplain.Text))
            {
                MessageBox.Show("说明不能为空！");
                txbConditionExplain.Focus();
                return;
            }
            SYS_QuoteLawConditionBLL sqlc = new SYS_QuoteLawConditionBLL();
            if (sqlc.UpdateSys_QuoteLawCondition(new SYS_QuoteLawCondition { QLC_ID = QLC_ID1, QLC_Name = txbConditionName.Text.Trim(), QLC_Content = colFileter1.GetFileter(), QLC_Explain = txbConditionExplain.Text.Trim() }))
            {
                MessageBox.Show("修改成功！");
                ShowTab_RelLaw();
                return;
            }
            else
            {
                MessageBox.Show("修改失败！");
                return;
            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            StringBuilder sb = new StringBuilder();
            foreach (DataGridViewColumn item in DV_QuoteLawRel.Columns)
            {
                if (item.HeaderText == "法规名称")
                {
                    sb.Append(item.HeaderText + ",");
                    dic.Add("法规名称", item.Name);
                }
                else if (item.HeaderText == "条名称")
                {
                    sb.Append(item.HeaderText + ",");
                    dic.Add("条名称", item.Name);
                }
                else if (item.HeaderText == "款名称")
                {
                    sb.Append(item.HeaderText + ",");
                    dic.Add("款名称", item.Name);
                }
                else if (item.HeaderText == "项名称")
                {
                    sb.Append(item.HeaderText + ",");
                    dic.Add("项名称", item.Name);
                }
                else if (item.HeaderText == "法规类别")
                {
                    sb.Append(item.HeaderText + ",");
                    dic.Add("法规类别", item.Name);
                }
                else if (item.HeaderText == "标记")
                {
                    sb.Append(item.HeaderText + ",");
                    dic.Add("标记", item.Name);
                }
                else if (item.HeaderText == "配置名称")
                {
                    sb.Append(item.HeaderText + ",");
                    dic.Add("配置名称", item.Name);
                }
            }
            string str = sb.ToString();
            Form3 f = new Form3(str);
            if (f.ShowDialog() == DialogResult.Yes)
            {
                string stt = f.Condition;
                //.Replace("配置名称", dic["配置名称"]
                string stt1 = stt.Replace("法规名称", dic["法规名称"]).Replace("条名称", dic["条名称"]).Replace("款名称", dic["款名称"]).Replace("项名称", dic["项名称"]).Replace("法规类别", dic["法规类别"]).Replace("标记", dic["标记"]).ToString();
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow[] dr = dt.Select(stt1);
                    if (dr.Length > 0)
                    {
                        DV_QuoteLawRelZH.DataSource = dt.Select(stt1).CopyToDataTable<DataRow>();
                    }
                }
                else
                {
                    DV_QuoteLawRelZH.DataSource = new DataTable();
                }
                for (int i = 0; i < DV_QuoteLawRelZH.Rows.Count; i++)
                {
                    DV_QuoteLawRelZH.Rows[i].Cells["OLR_Law_ClauseName1"].Value = Convert.ToInt32(string.IsNullOrEmpty(dt.Rows[i]["OLR_Law_ClauseNumber"].ToString()) ? 0 : dt.Rows[i]["OLR_Law_ClauseNumber"]);
                    DV_QuoteLawRelZH.Rows[i].Cells["OLR_Law_ItemName1"].Value = Convert.ToInt32(string.IsNullOrEmpty(dt.Rows[i]["OLR_Law_ItemNumber"].ToString()) ? 0 : dt.Rows[i]["OLR_Law_ItemNumber"]);
                }

            }
        }

        private void SelectWord_KeyPress(object sender, KeyPressEventArgs e)
        {
            SelectWord.Focus();
        }

        private void button7_Click_1(object sender, EventArgs e)
        {

        }

        private void button4_Click_1(object sender, EventArgs e)
        {

        }

        private void btnXZZF_Click(object sender, EventArgs e)
        {
            TypeName = "行政执法";
            GetColor();
            var sendButton = (Button)sender;
            if (sendButton != null)
                sendButton.BackColor = Color.LightSkyBlue;
            CaseCauseTree();
        }

        /// <summary>
        /// 主题词关联法条ID
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                //选择框不为对勾和×时
                if (e.ColumnIndex != 25 && e.ColumnIndex != 26)
                {
                    string OLR_Law_ProvID = dataGridView1.Rows[e.RowIndex].Cells["ProvInID"].Value.ToString();
                    richTextBoxFTContent.Text = quoteLawRelBLL.GetProvText(OLR_Law_ProvID);
                    txbWord.Text = "";
                    tvZTC.Nodes.Clear();
                    var node = new TreeNode();
                    node.Text = dataGridView1.Rows[e.RowIndex].Cells["articleName"].Value.ToString();
                    node.Tag = dataGridView1.Rows[e.RowIndex].Cells["ProvInID"].Value.ToString() + "," + dataGridView1.Rows[e.RowIndex].Cells["ProvNumber"].Value.ToString() + ",条";
                    if (quoteLawRelBLL.GetNumByProvId(Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["ProvInID"].Value)))
                    {
                        node.Nodes.Add(new TreeNode());
                    }
                    tvZTC.Nodes.Add(node);
                }
            }
        }

        /// <summary>
        /// 定位事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            richTextBoxFTContent.SelectionStart = 0;
            richTextBoxFTContent.SelectionLength = richTextBoxFTContent.Text.Length;
            richTextBoxFTContent.SelectionBackColor = Color.White;
            MatchCollection mc;
            var r = new System.Text.RegularExpressions.Regex(txbWord.Text.Trim()); //定义一个Regex对象实例
            mc = r.Matches(richTextBoxFTContent.Text);
            for (var i = 0; i < mc.Count; i++) //在输入字符串中找到所有匹配
            {
                var start = mc[i].Index; //记录匹配字符的位置
                var length = (txbWord.Text.Trim()).Length;
                richTextBoxFTContent.SelectionStart = start;
                richTextBoxFTContent.SelectionLength = length;
                richTextBoxFTContent.SelectionBackColor = Color.LightSkyBlue;
            }
        }

        /// <summary>
        /// 主题词关联法条下级树展开事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvZTC_AfterExpand(object sender, TreeViewEventArgs e)
        {
            e.Node.Nodes.Clear();
            CaseCauseTreeChild1(e.Node);
        }

        /// <summary>
        /// 下级树
        /// </summary>
        /// <param name="parentNode"></param>
        private void CaseCauseTreeChild1(TreeNode Node)
        {
            //ProvInID,ProvNumber,ProvText,ArticleName as ProvInName
            List<T_Law_Article> laws = new List<T_Law_Article>();
            string pid = Node.Tag.ToString();// 父级ID
            string[] dr = pid.Split(',');
            pid = dr[0];
            string name = dr[2].ToString();
            if (name == "条")
            {
                //获取款或者项信息根据条ID
                laws = quoteLawRelBLL.GetLawClauseData(Convert.ToInt32(pid));
            }
            else if (name == "款")
            {
                //获取项信息根据款ID
                laws = quoteLawRelBLL.GetLawItemData(Convert.ToInt32(pid));
            }
            if (laws == null || laws.Count == 0)
                return;
            laws.ForEach(x =>
            {
                var node = new TreeNode();
                node.Text = x.LP_ArticleName;
                node.Name = x.LP_Text;
                node.Tag = x.LP_ID + "," + x.LP_Type == "91" ? x.LP_Paragraph_Number + ",款" : x.LP_Item_Number + ",项";
                //
                if (x.LP_Type == "91" && quoteLawRelBLL.GetLawItemData(x.LP_ID).Count > 0)
                {
                    node.Nodes.Add(new TreeNode());
                }
                Node.Nodes.Add(node);
            });
        }

        /// <summary>
        /// 主题词关联法条选中树事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvZTC_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1 && e.Node.Checked == true)
            {
                string provInId = "", provInName = "", provNunber = "", clauseInId = "0", clauseNum = "", clauseName = "", ItemID = "0", ItemNumber = "", ItemName = "", BasisType = "";
                string tag = e.Node.Tag.ToString();
                string[] str = tag.Split(',');
                tag = str[0].ToString();
                string typename = str[2].ToString();
                if (typename == "条")
                {
                    //provInId = tag;
                    //// name = e.Node.Name.ToString();
                    //provNunber = str[1].ToString();
                    //provInName = e.Node.Text.ToString();
                    //选中条之后把已添加的款和项去掉
                    dataGridView1.SelectedRows[0].Cells["clauseNum"].Value = clauseNum;
                    dataGridView1.SelectedRows[0].Cells["clauseName"].Value = clauseName;
                    dataGridView1.SelectedRows[0].Cells["ItemNumber"].Value = ItemNumber;
                    dataGridView1.SelectedRows[0].Cells["ItemName"].Value = ItemName;
                }
                else if (typename == "款")
                {
                    provInName = e.Node.Parent.Text.ToString();
                    string[] str1 = e.Node.Parent.Tag.ToString().Split(',');
                    provInId = str1[0].ToString();
                    provNunber = str1[1].ToString();
                    clauseInId = tag;
                    clauseName = e.Node.Text.ToString();
                    clauseNum = str[1].ToString();
                    dataGridView1.SelectedRows[0].Cells["clauseNum"].Value = clauseNum;
                    dataGridView1.SelectedRows[0].Cells["clauseName"].Value = clauseName;
                }
                else if (typename == "项")
                {
                    provInName = e.Node.Parent.Parent.Text.ToString();
                    string[] str2 = e.Node.Parent.Parent.Tag.ToString().Split(',');
                    provInId = str2[0].ToString();
                    provNunber = str2[1].ToString();
                    clauseName = e.Node.Parent.Text.ToString();
                    string[] str1 = e.Node.Parent.Tag.ToString().Split(',');
                    clauseInId = str1[0].ToString();
                    clauseNum = str1[1].ToString();
                    ItemNumber = tag;
                    ItemName = e.Node.Text.ToString();
                    dataGridView1.SelectedRows[0].Cells["clauseNum"].Value = clauseNum;
                    dataGridView1.SelectedRows[0].Cells["clauseName"].Value = clauseName;
                    dataGridView1.SelectedRows[0].Cells["ItemNumber"].Value = ItemNumber;
                    dataGridView1.SelectedRows[0].Cells["ItemName"].Value = ItemName;
                }
            }
        }

        //保存主题词关联法条
        private void btnSaveZTC_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                SYS_QuoteLawRel sq = new SYS_QuoteLawRel
                {
                    QLR_ID = "QLR" + DateTime.Now.ToString("yyyyMMddHHmmss"),
                    OLR_CaseCauseID = clicknode.Name.ToString() == "2" ? clicknode.Tag.ToString() : ccb.IsParentRealation1(clicknode.Tag.ToString()).CC_ID,
                    OLR_SituationTypeID = Convert.ToInt32(clicknode.Name.ToString()),
                    OLR_SituationID = clicknode.Tag.ToString(),
                    OLR_LawID = dataGridView1.SelectedRows[0].Cells["CATInID"].Value.ToString(),
                    OLR_LawName = dataGridView1.SelectedRows[0].Cells["Standardcatname"].Value.ToString(),
                    OLR_Law_ProvID = dataGridView1.SelectedRows[0].Cells["ProvInID"].Value.ToString(),
                    OLR_Law_ProvName = dataGridView1.SelectedRows[0].Cells["articleName"].Value.ToString(),
                    OLR_Law_ClauseNumber = dataGridView1.SelectedRows[0].Cells["clauseNum"].Value.ToString(),
                    //string.IsNullOrEmpty() ? "" : quoteLawRelDataTable.Rows[i]["ClauseNumber"].ToString() == "0" ? "" : quoteLawRelDataTable.Rows[i]["ClauseNumber"].ToString(),
                    //OLR_Law_ClauseNumber = string.IsNullOrEmpty(dataGridViewData.Rows[i].Cells["dataGridViewTextBoxColumn22"].Value.ToString()) ? "" : dataGridViewData.Rows[i].Cells["dataGridViewTextBoxColumn22"].Value.ToString() == "0" ? "" : dataGridViewData.Rows[i].Cells["dataGridViewTextBoxColumn22"].Value.ToString(),
                    OLR_Law_ClauseName = dataGridView1.SelectedRows[0].Cells["clauseName"].Value.ToString(),
                    //dataGridViewData.Rows[i].Cells["dataGridViewTextBoxColumn22"].FormattedValue == null ? "" : dataGridViewData.Rows[i].Cells["dataGridViewTextBoxColumn22"].FormattedValue.ToString(),
                    //OLR_Law_ItemNumber = string.IsNullOrEmpty(quoteLawRelDataTable.Rows[i]["ItemNumber"].ToString()) ? "" : quoteLawRelDataTable.Rows[i]["ItemNumber"].ToString() == "0" ? "" : quoteLawRelDataTable.Rows[i]["ItemNumber"].ToString(),
                    //OLR_Law_ItemNumber = string.IsNullOrEmpty(dataGridViewData.Rows[i].Cells["dataGridViewTextBoxColumn23"].Value.ToString()) ? "" : dataGridViewData.Rows[i].Cells["dataGridViewTextBoxColumn23"].Value.ToString() == "0" ? "" : dataGridViewData.Rows[i].Cells["dataGridViewTextBoxColumn23"].Value.ToString(),
                    // OLR_Law_ItemName = dataGridViewData.Rows[i].Cells["dataGridViewTextBoxColumn23"].FormattedValue == null ? "" : dataGridViewData.Rows[i].Cells["dataGridViewTextBoxColumn23"].FormattedValue.ToString(),
                    OLR_Law_ItemNumber = dataGridView1.SelectedRows[0].Cells["ItemNumber"].Value.ToString(),
                    OLR_Law_ItemName = dataGridView1.SelectedRows[0].Cells["ItemName"].Value.ToString(),
                    OLR_Law_HWType = dataGridView1.SelectedRows[0].Cells["HWType"].Value.ToString(),
                    OLR_Law_HWName = dataGridView1.SelectedRows[0].Cells["HWName"].Value.ToString(),
                    OLR_Law_PromuDate = Convert.ToDateTime(dataGridView1.SelectedRows[0].Cells["PromuDate"].Value.ToString()),
                    OLR_Law_LostFlag = dataGridView1.SelectedRows[0].Cells["LostFlag"].Value.ToString(),
                    OLR_Law_ProvNumber = dataGridView1.SelectedRows[0].Cells["ProvNumber"].Value.ToString(),
                    OLR_LawFlag = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["lawFlag"].Value.ToString()),
                    OLR_LawEffectiveDate = Convert.ToDateTime(dataGridView1.SelectedRows[0].Cells["EffectiveDate"].Value.ToString()),
                    QLR_IsBasicLaw = string.IsNullOrEmpty(dataGridView1.SelectedRows[0].Cells["IsBasicLaw"].Value.ToString()) ? DBNull.Value : dataGridView1.SelectedRows[0].Cells["IsBasicLaw"].Value,
                    HCode = dataGridView1.SelectedRows[0].Cells["HCode"].Value.ToString(),
                    QLR_ProvType = dataGridView1.SelectedRows[0].Cells["BasisType"].Value.ToString()
                };
                sq.OLR_LevelID = !string.IsNullOrEmpty(sq.OLR_Law_ItemNumber) ? 4 : !string.IsNullOrEmpty(sq.OLR_Law_ClauseNumber) ? 3 : !string.IsNullOrEmpty(sq.OLR_Law_ProvID) ? 2 : 1;
                if (!quoteLawRelBLL.IsExistsP("行政执法", clicknode.Tag.ToString(), sq.OLR_Law_ProvID, sq.OLR_Law_ClauseNumber, sq.OLR_Law_ItemNumber, ""))
                {

                    bool res = quoteLawRelBLL.Add(sq);
                    if (res)
                    {
                        MessageBox.Show("添加成功！");
                    }
                    else
                    {
                        MessageBox.Show("添加失败！");
                    }
                }
                else
                {
                    MessageBox.Show("该数据已存在！");
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                //Log.WriteLog("当前选择第" + e.ColumnIndex + "列");
                if (e.ColumnIndex == 25 || e.ColumnIndex == 26)
                {
                    dataGridView1.EndEdit();
                    DataGridViewCheckBoxCell checkCellRight = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells["right"];
                    DataGridViewCheckBoxCell checkCellwrong = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells["wrong"];
                    //Log.WriteLog("对勾框状态" + checkCellRight.Value);
                    //Log.WriteLog("错误框状态" + checkCellwrong.Value);
                    string ProvHwordInID = dataGridView1.Rows[e.RowIndex].Cells["ProvHwordInID"].Value.ToString();
                    if (e.ColumnIndex == 25)
                    {
                        if (checkCellRight.Value != DBNull.Value && Convert.ToBoolean(checkCellRight.EditedFormattedValue) == true)
                        {
                            checkCellRight.Value = true;
                            checkCellwrong.Value = false;

                        }
                        else
                        {
                            checkCellRight.Value = false;
                            checkCellwrong.Value = true;
                        }
                    }
                    else if (e.ColumnIndex == 26)
                    {
                        if (checkCellwrong.Value != DBNull.Value && Convert.ToBoolean(checkCellwrong.EditedFormattedValue) == true)
                        {
                            checkCellRight.Value = false;
                            checkCellwrong.Value = true;
                        }
                        else
                        {
                            checkCellRight.Value = true;
                            checkCellwrong.Value = false;
                        }
                    }
                    QuoteLawSet qs = new QuoteLawSet() { ProvHwordInID = ProvHwordInID, checkright = checkCellRight.Value != DBNull.Value && Convert.ToBoolean(checkCellRight.Value.ToString()) == true ? 1 : 0, checkwrong = checkCellwrong.Value != DBNull.Value && Convert.ToBoolean(checkCellwrong.Value.ToString()) == true ? 1 : 0 };
                    if (quoteLawRelBLL.IsExistsQuoteLawSet(ProvHwordInID))
                    {
                        quoteLawRelBLL.UpdateQuoteLawSet(qs);
                    }
                    else
                    {
                        quoteLawRelBLL.AddQuoteLawSet(qs);
                    }
                }
            }
        }
    }




}